<template>
  <el-form :model="form" label-width="auto" style="max-width: 600px">
    <el-form-item label="name">
      <el-input v-model="form.name" />
    </el-form-item>
    <el-form-item label="role">
       <el-tree
    style="max-width: 600px"
    :props="props"
    :data="data"
    show-checkbox
    @check-change="handleCheckChange"
    ref="box"
  />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">Create</el-button>
      <el-button>Cancel</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive,ref } from 'vue'

// do not use same name with ref
const form = reactive({
  name: '',
  auth:[]
})

import {roleadd,getroleadd} from '../../request/index'
import toTree from '../../utils/toTree'
const data=ref([])
roleadd().then(res=>{
  console.log('roleadd',res)
  data.value=toTree(res.data.data)
})


const props = {
  label: 'name',
  children: 'children',
}

const box=ref()

const onSubmit = () => {
let check=box.value.getCheckedNodes(false,true)
form.auth=check.map(item=>item.id)
  getroleadd(form).then(res=>{
    console.log('radd',res)
  })
}
</script>
